<?php
/**
 * 权限
 */
namespace app\model;

use think\Model;
use app\libs\Predis;

class ChainModel extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'chain';
    
    // 设置字段信息
    protected $schema111 = [
        'id'          => 'int',
        'chain_name'        => 'string',
    ];
    
    // 设置当前模型的数据库连接
    protected $connection ='db_yunduan';
   
   /**
    * 递归
    */
    public function GetInfo($id=0){

        $cache = new Predis();
        $data=$cache->get('ChainDiGui_'.$id);
        if($data){
            $result=unserialize($data);
        }else{
            $chain = new ChainModel();
            $res=$chain->field('id,chain_name,pid')->where(['status'=>1])->select()->toArray();
    
            $result=arr2tree($res,$id,'chain_name','id');
            $cache->set('ChainDiGui_'.$id,serialize($result),3600);
        }
        return $result;        
    }
    /**
     * 详情
     */
    public function GetDetail($id=0){
    	$chain = new ChainModel();
    	$result=$chain->field('id,chain_name,imgs')->where(['status'=>1])->find($id);
    	if(!empty($result)){
    		if(empty($result['imgs'])){
    			$result['imgs']=[];
    		}else{
    			$result['imgs']=\json_decode($result['imgs'],true);
    		}
    	}
    	return $result;
    }
    /**
     * 获取政策的产业名称
     */
    public function GetChainNameByPolicy($id){
    	$ChainModel=new ChainModel;
    	$result=$ChainModel->alias('a')->join('policy_chain b','a.id=b.chain_id','inner')->join('chain c','b.chain_id=c.id','inner')->where(['b.policy_id'=>$id,'b.status'=>1,'a.status'=>1,'c.pid'=>0])->column('a.chain_name');
    	return $result;
    }
    

}
 